package com.king.zk.app;

import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.List;

/**
 * 客户端性质开发:
 * 1. 客户端对象
 * 2. 调用API方法
 * 3. 关闭
 */
public class TestZookeeper {
    private String connectString = "hadoop102:2181,hadoop103:2181,hadoop104:2181" ;
    private int sessionTimeout = 10000 ;
    private ZooKeeper zookeeper ;

    /**
     * 删除
     *
     * 扩展: 递归删除非空的Znode
     */
    @Test
    public void testDelete() throws KeeperException, InterruptedException {
        zookeeper.delete("/bigdata0523/ningge",-1);
        //zookeeper.delete("/bigdata0523",-1);
    }

    /**
     * 设置节点内容
     */
    @Test
    public void testSetData() throws KeeperException, InterruptedException {
        //Stat stat = zookeeper.setData("/bigdata0523", "shuaigemeinv".getBytes(), getStat("/bigdata0523").getVersion());
        //Stat stat = zookeeper.setData("/bigdata0523", "dashenban".getBytes(), 5);
        Stat stat = zookeeper.setData("/bigdata0523", "dashenban".getBytes(), -1);

    }

    /**
     * 获取节点的内容
     */
    @Test
    public void testGetData() throws KeeperException, InterruptedException {
        byte[] data = zookeeper.getData("/bigdata0523", false, getStat("/bigdata0523"));

        System.out.println("data:" + new String(data));
    }

    public Stat getStat(String path) throws KeeperException, InterruptedException {
        return zookeeper.exists(path,false);
    }

    /**
     * 判断节点是否存在
     */
    @Test
    public void testExists() throws KeeperException, InterruptedException {
        Stat stat = zookeeper.exists("/bigdata0523", false);
        System.out.println("stat = " + stat);
    }



    /**
     * 获取Znode的子节点
     */
    @Test
    public void  testListZnodes() throws KeeperException, InterruptedException {
        // 不监听
       //List<String> children = zookeeper.getChildren("/bigdata0523", false);
       //System.out.println("children = " + children);

        //监听
        List<String> children = zookeeper.getChildren("/bigdata0523", new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                // 当监听的事件发生后，在此方法中做事件的处理
                System.out.println("监听到/bigdata0523的子节点发生变化");
            }
        });

        System.out.println("children = " + children);

        //保持方法不结束
        Thread.sleep(Long.MAX_VALUE);
    }

    /**
     * 创建Znode
     * @throws KeeperException
     * @throws InterruptedException
     */
    @Test
    public void testCreate() throws KeeperException, InterruptedException {
        String s = zookeeper.create("/bigdata0523/lihefei", "kuai".getBytes(),
                ZooDefs.Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT);
        System.out.println("s = " + s);
    }




    @Before
    public void init() throws IOException {
        zookeeper  = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
            @Override
            public void process(WatchedEvent event) {

            }
        });
    }

    @After
    public void close() throws InterruptedException {
        zookeeper.close();
    }


    @Test
    public void testZookeeper() throws IOException, InterruptedException {
        //三个参数
        //1. connectString  指定Zookeeper集群的位置
        //2. sessionTimeout 客户端的超时时间
        //3. Watcher  默认的监听对象.
        String connectString = "hadoop102:2181,hadoop103:2181,hadoop104:2181" ;
        // minSessionTimeout =4000  <= 指定的sessionTimeout <=  maxSessionTimeout=40000
        int sessionTimeout = 10000 ;
        ZooKeeper zooKeeper = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                //对事件处理的过程
            }
        });
        System.out.println("zooKeeper = " + zooKeeper);

        zooKeeper.close();
    }




}


















